package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.risk;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.QuotaConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.StrategyConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.accept.master.InsuranceAcceptRepository;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * description:
 *
 * @author jackdaw
 * @date 2020-06-17 17:07
 */
@Component
public class Lwt006InsuranceRiskStrategy implements InsuranceRiskStrategy {

    private final InsuredCoreRepository insuredCoreRepository;

    private final RedisService redisService;

    private final InsuranceAcceptRepository insuranceAcceptRepository;

    public Lwt006InsuranceRiskStrategy(InsuredCoreRepository insuredCoreRepository, RedisService redisService, InsuranceAcceptRepository insuranceAcceptRepository) {
        this.insuredCoreRepository = insuredCoreRepository;
        this.redisService = redisService;
        this.insuranceAcceptRepository = insuranceAcceptRepository;
    }

    @Override
    public InsuredModel checkProductRiskQuota(ProductModel product, RequestModel requestModel) {
        //当前单保费
        Integer payYear = product.getPayYear();
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(product.getProdInsSeq(), insurantModelList);
        Date valiDate = requestModel.getPolicyInfo().getCvalidate();
        int days = DateUtil.getDays(insurantModel.getBirthday(), valiDate);
        int years = DateUtil.getYears(insurantModel.getBirthday(), valiDate);
        if (StringUtils.equals(StrategyConstant.AGE, product.getPayYearFlag())) {
            payYear = payYear - years;
        }
        BigDecimal currentPrem = product.getPrem();
        if (!StringUtils.equals(requestModel.getProductInfo().getPayIntv(), UnderWriteConstant.SINGLE_PAY)) {
            currentPrem = product.getPrem().multiply(BigDecimal.valueOf(payYear)).multiply(BigDecimal.valueOf(12 / Integer.parseInt(requestModel.getProductInfo().getPayIntv())));
        }
        BigDecimal cachePrem = redisService.getCacheQuota(QuotaConstant.SORT_LBP030, insurantModel.getIdNo(), currentPrem, requestModel.getPolicyInfo().getContNo());
        BigDecimal historicalPrem = insuredCoreRepository.getHistoricalJbhlPrem(insurantModel.getInsuredNo());
        BigDecimal total = currentPrem.add(cachePrem).add(historicalPrem);
        // 核保时匹配姓名和证件号，如匹配一致，则可在该客户超额累计保费额度内投保
        String insuredQuota = insuredCoreRepository.getWhiteVerify(insurantModel.getIdNo(),
                insurantModel.getName(), requestModel.getPolicyInfo().getAgencyCode());
        if (StringUtils.isNotBlank(insuredQuota)) {
            BigDecimal prem = BigDecimal.valueOf(Double.parseDouble(insuredQuota));
            if (total.compareTo(prem) > 0) {
                return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                        String.format(UnderWriteConstant.UNDER_WRITE_White_MESSAGE, insurantModel.getName(), prem));
            }
        } else {
            //判断客户等级 1或空-普通客户 2-私行客户 3-特殊客户
            String cusType = AcceptConstant.NORMAL;
            if (!StringUtils.isBlank(insurantModel.getInsuredNo()) && !AcceptConstant.MINUS_ONE.equals(insurantModel.getInsuredNo())) {
                cusType = insuredCoreRepository.getCustomerType(insurantModel.getInsuredNo());
            }
            if (StringUtils.isBlank(cusType) || AcceptConstant.NORMAL.equals(cusType)) {
                //开门红优惠活动
                //获取优惠时间区间
                String area = insuranceAcceptRepository.activityTime("KMH2021");
                //获取保单申请时间
                Date polApplyDate = requestModel.getPolicyInfo().getPolApplyDate();
                boolean isTimeArea = false;
                try {
                    isTimeArea = AcceptUtil.isTimeArea(area, polApplyDate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (isTimeArea) {
                    if (days >= UnderWriteConstant.AGE_THIRTY_DAY && years <= UnderWriteConstant.AGE_SIXTY_YEAR && total.compareTo(BigDecimal.valueOf(5 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.KMH_UNDER_WRITE_NYGG0051_MESSAGE, insurantModel.getName(), years));
                    } else if (years >= UnderWriteConstant.AGE_SIXTY_ONE_YEAR && years <= UnderWriteConstant.AGE_SEVENTY_YEAR && total.compareTo(BigDecimal.valueOf(2.5 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.KMH_UNDER_WRITE_NYGG0052_MESSAGE, insurantModel.getName(), years));
                    }
                } else {
                    if (days >= UnderWriteConstant.AGE_THIRTY_DAY && years <= UnderWriteConstant.AGE_FIFTY_YEAR && total.compareTo(BigDecimal.valueOf(5 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.KMH_UNDER_WRITE_NYGG0051_MESSAGE, insurantModel.getName(), years));
                    } else if (years >= UnderWriteConstant.AGE_FIFTY_ONE_YEAR && years <= UnderWriteConstant.AGE_SEVENTY_YEAR && total.compareTo(BigDecimal.valueOf(3 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.UNDER_WRITE_NYGG0051_MESSAGE, insurantModel.getName(), years));
                    }
                }
            } else if (AcceptConstant.PRIVATE.equals(cusType)) {
                //查询时间区间
                String area = insuranceAcceptRepository.activityTime("PRIVATEG");
                //获取保单申请时间
                Date polApplyDate = requestModel.getPolicyInfo().getPolApplyDate();
                boolean isTimeArea = false;
                try {
                    isTimeArea = AcceptUtil.isTimeArea(area, polApplyDate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (isTimeArea) {
                    if (days >= UnderWriteConstant.AGE_THIRTY_DAY && years <= UnderWriteConstant.AGE_FIFTY_FIVE_YEAR && total.compareTo(BigDecimal.valueOf(15 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.NORMAL_TEMPLATE_PREM, insurantModel.getName(), years, 1500));
                    } else if (years >= UnderWriteConstant.AGE_FIFTY_SIX_YEAR && years <= UnderWriteConstant.AGE_SIXTY_YEAR && total.compareTo(BigDecimal.valueOf(10 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.NORMAL_TEMPLATE_PREM, insurantModel.getName(), years, 1000));
                    } else if (years >= UnderWriteConstant.AGE_SIXTY_ONE_YEAR && years <= UnderWriteConstant.AGE_SEVENTY_YEAR && total.compareTo(BigDecimal.valueOf(3 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.NORMAL_TEMPLATE_PREM, insurantModel.getName(), years, 300));
                    }
                } else {
                    if (days >= UnderWriteConstant.AGE_THIRTY_DAY && years < UnderWriteConstant.AGE_EIGHTEEN_YEAR && total.compareTo(BigDecimal.valueOf(5 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.NORMAL_TEMPLATE_PREM, insurantModel.getName(), years, 500));
                    } else if (years >= UnderWriteConstant.AGE_EIGHTEEN_YEAR && years <= UnderWriteConstant.AGE_FIFTY_YEAR && total.compareTo(BigDecimal.valueOf(10 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.NORMAL_TEMPLATE_PREM, insurantModel.getName(), years, 1000));
                    } else if (years >= UnderWriteConstant.AGE_FIFTY_ONE_YEAR && years <= UnderWriteConstant.AGE_FIFTY_FIVE_YEAR && total.compareTo(BigDecimal.valueOf(8 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.NORMAL_TEMPLATE_PREM, insurantModel.getName(), years, 800));
                    } else if (years >= UnderWriteConstant.AGE_FIFTY_SIX_YEAR && years <= UnderWriteConstant.AGE_SEVENTY_YEAR && total.compareTo(BigDecimal.valueOf(3 * UnderWriteConstant.ONE_MILLION)) > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                                String.format(UnderWriteConstant.NORMAL_TEMPLATE_PREM, insurantModel.getName(), years, 300));
                    }
                }

            } else if (AcceptConstant.SPECIAL.equals(cusType)) {
                return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
            } else {
                return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, AcceptConstant.CUSTOMER_TYPE_ERROR);
            }

        }

        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
}
